package com.yyk.yiliao.ui.activity.detail.fragment;


import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseFragment;
import com.yyk.yiliao.adapter.Detail_F_Three_Fragment;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.bean.Hlist_Info;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.ToastUtil;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.widget.diver.CustomDecoration;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.Unbinder;
import retrofit2.HttpException;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ${zk} on 2018/8/14 0014.
 */

public class Detail_Fragment_Three extends BaseFragment {
    public static final String ARGS_PAGE = "Detail_Fragment_Three";

    Unbinder unbinder;
    @BindView(R.id.nRb)
    RadioButton  nRb;
    @BindView(R.id.nRb2)
    RadioButton  nRb2;
    @BindView(R.id.nRb3)
    RadioButton  nRb3;
    @BindView(R.id.nRb4)
    RadioButton  nRb4;
    @BindView(R.id.nRg)
    RadioGroup   nRg;
    @BindView(R.id.nRv)
    RecyclerView nRv;

    public static Detail_Fragment_Three newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARGS_PAGE, page);
        Detail_Fragment_Three fragment = new Detail_Fragment_Three();
        fragment.setArguments(args);
        return fragment;
    }
    private void initGetIntent() {
        Bundle arguments = getArguments();
        int id = arguments.getInt("id");
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_detail_three;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        setView();

    }

    private void setView() {
//        initRadioGroup();
//        setPinglun();
    }

    private void setPinglun() {
        Map<String, String> map = new TreeMap<>();

        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postUrlHlist(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Subscriber<Hlist_Info>() {

                    @Override
                    public void onCompleted() {
                        //完成的时候调用
                        //                        mView.onComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        //失败的时候调用-----一下可以忽略 直接 callBack.onFaild("请求失败");
                        if (e instanceof HttpException) {
                            HttpException httpException = (HttpException) e;
                            //httpException.response().errorBody().string()
                            int code = httpException.code();
                            if (code == 500 || code == 404) {
                                ToastUtil.showShort(getActivity(), "服务器出错");
                            }
                        } else if (e instanceof ConnectException) {
                            ToastUtil.showShort(getActivity(), "网络断开,请打开网络!");
                        } else if (e instanceof SocketTimeoutException) {
                            ToastUtil.showShort(getActivity(), "网络连接超时!!");

                        } else {
                            ToastUtil.showShort(getActivity(), "发生未知错误" + e.getMessage());
//                            Logger.e("MyFind", e.getMessage());
                        }
                    }

                    @Override
                    public void onNext(Hlist_Info info) {

                        if (info.getCode() == 1) {
                            final List<Hlist_Info.DataBean> data = info.getData();
                            final List<Integer> list = new ArrayList<>();
                            list.add(R.mipmap.img_pltp);
                            list.add(R.mipmap.img_pltp);
                            //成功------获取完数据,返回给P---P获取到数据之后就将数据交回给V
                            BaseRecyclerAdapter<Hlist_Info.DataBean> adapter = new BaseRecyclerAdapter<Hlist_Info.DataBean>(getActivity(), data, R.layout.adapter_item_detail_pinglun) {
                                @Override
                                public void convert(BaseRecyclerHolder holder, Hlist_Info.DataBean item, int position, boolean isScrolling) {
                                    Hlist_Info.DataBean dataBean = data.get(position);
                                    GridView gridView = holder.getView(R.id.gridView);
                                    gridView.setAdapter(new Detail_F_Three_Fragment(getContext(), list));

                                }
                            };
                            nRv.addItemDecoration(new CustomDecoration(getContext(), CustomDecoration.VERTICAL_LIST, R.drawable.divider_f8_10, 0));
                            nRv.setLayoutManager(new LinearLayoutManager(getActivity()));
                            nRv.setAdapter(adapter);
                        } else {
                            //失败
                        }
                    }


                });
        try {
            //因为网络请求在子线程，所以不能让宿主线程过早的结束。
            Thread.sleep(100);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }

    /**
     * 评论切换
     */
    private void initRadioGroup() {
        nRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                    case R.id.nRb:
                        RadioButton radioButton = group.findViewById(R.id.nRb);
                        String string = radioButton.getText().toString();
                        ToastUtil.showShort(getActivity(), string);
                        break;
                    case R.id.nRb2:
                        String string2 = ((RadioButton) group.findViewById(R.id.nRb2)).getText().toString().trim();
                        ToastUtil.showShort(getActivity(), string2);

                        break;
                    case R.id.nRb3:
                        String string3 = ((RadioButton) group.findViewById(R.id.nRb3)).getText().toString().trim();

                        ToastUtil.showShort(getActivity(), string3);

                        break;
                    case R.id.nRb4:
                        String string4 = ((RadioButton) group.findViewById(R.id.nRb4)).getText().toString().trim();

                        ToastUtil.showShort(getActivity(), string4);

                        break;

                }
            }
        });
    }


}
